package com.wudl.govern.controller;

import com.wudl.common.api.CommonResult;

import com.wudl.govern.domain.MetaTableHistory;
import com.wudl.govern.service.MetaTableHistoryService;
import com.wudl.govern.vo.MetaTableHistoryVO;
import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;
import java.util.ArrayList;

/**
 * meta_table_history Service
 *
 * @author wudl
 */
@Api
@RestController
@RequestMapping("/metaTableHistory")
public class MetaTableHistoryController {

    @Autowired
    private MetaTableHistoryService metaTableHistoryService;

    /**
     * 分页查询
     */
    @PostMapping("/page")
    public CommonResult<List<MetaTableHistoryVO>> page(@RequestBody MetaTableHistoryVO pageVO, @RequestParam Integer page, @RequestParam Integer size) {

        List<MetaTableHistoryVO> MetaTableHistoryVoList = new ArrayList<>();
  // 拷贝结果
        List<MetaTableHistory> MetaTableHistoryList = metaTableHistoryService.page(page, size);

        for (MetaTableHistory metaTableHistoryTmp:MetaTableHistoryList )
        {
            MetaTableHistoryVO metaTableHistoryVO = new MetaTableHistoryVO();
            BeanUtil.copyProperties(metaTableHistoryTmp, MetaTableHistoryVO.class);
            MetaTableHistoryVoList.add(metaTableHistoryVO);
        }
        return CommonResult.success(MetaTableHistoryVoList);


    }

    /**
     * 查询详情
     */
    @GetMapping("/get/{id}")
    public CommonResult<MetaTableHistoryVO> get(@PathVariable Integer id) {
        MetaTableHistory entity = metaTableHistoryService.getById(id);
        return CommonResult.success(BeanUtil.copyProperties(entity, MetaTableHistoryVO.class));
    }

    /**
     * 新增
     */
    @PostMapping("/add")
    public CommonResult<Void> add(@RequestBody MetaTableHistoryVO addVO) {
        metaTableHistoryService.save(BeanUtil.copyProperties(addVO, MetaTableHistory.class));
        return CommonResult.success();
    }

    /**
     * 编辑
     */
    @PutMapping("/edit")
    public CommonResult<Void> edit(@RequestBody MetaTableHistoryVO editVO) {
        metaTableHistoryService.updateById(BeanUtil.copyProperties(editVO, MetaTableHistory.class));
        return CommonResult.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public CommonResult<Void> delete(@PathVariable Integer id) {
        metaTableHistoryService.removeById(id);
        return CommonResult.success();
    }

}
